package com.ecoolex.ying.space.api.graphql;

import java.util.Date;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberManager;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.entity.PushMessage;
import com.ecoolex.ying.space.ws.PushMessageService;


@GraphQL2Module(type = Type.User, description = "推送查询")
public class PushMessageGraphql {

    @Reference
    private PushMessageService pushMessageService;

    @Autowired
    private StorageService storageService;

    @Autowired
    private ConsumerMemberManager consumerMemberManager;

    @TypeField(type = PushMessage.class, field = "releaseUser", description = "推送用户")
    public ConsumerMember getCommentUser(@Source PushMessage source) {
        return consumerMemberManager.getByUserId(source.getReleaseUserId());
    }

    @TypeField(type = PushMessage.class, field = "coverImage", description = "推送封面图")
    public StorageInfo coverImage(@Source PushMessage source) {
        return storageService.get(source.getCoverImage());
    }

    @TypeField(type = PushMessage.class, field = "unreadPushMessageCount", description = "未读推送数量")
    public Integer unreadPushMessageCount(@Source PushMessage source, ConsumerMemberAuthority user) {
        if (user.isLogin()) {
            return pushMessageService.unreadCount(user.userId(), source.getReleaseUserId());
        } else {
            return 0;
        }
    }

    @QueryField(field = "pushMessageList", description = "推送列表")
    public Pagination<PushMessage> query(@NonNull Date now, @NonNull Page page, ConsumerMemberAuthority user) {
        user.assertLogin();
        return pushMessageService.queryPushMessageForH5(now, page);
    }

    @QueryField(field = "pushMessage", description = "推送详情")
    public Pagination<PushMessage> get(@NonNull Integer releaseUserId, @NonNull Page page, ConsumerMemberAuthority user) {
        user.assertLogin();
        return pushMessageService.getPushMessageByReleaseUserIdForH5(releaseUserId, page);
    }

    @MutationField(field = "updatePushMessageReadState", description = "更新推送读取状态")
    public boolean updatePushMessageReadState(@NonNull Integer releaseUserId, ConsumerMemberAuthority user) {
        if (user.isLogin()) {
            return pushMessageService.updatePushMessageReadState(user.userId(), releaseUserId, user.userName());
        }
        return true;
    }
}
